/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.tree;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.Serializable;

import javax.swing.*;

import com.pearreview.ui.*;
import com.pearreview.application.Messages;
import com.pearreview.util.review.Aspect;


public class ListAspectForm extends JPanel {
	
	private static final long serialVersionUID = -2266807617507620713L;
	private FormLabel formHeading;
	private FormLabel nameLabel;
	private FormLabel categoryLabel;
	private FormLabel descriptionLabel;
	private CompletableJTextField name;
	private JComboBox category;
	//private FormLabel category;
	private JScrollPane scrollPane1;
	private FadingTextArea description;
	private JButton saveBtn;
	
	private Action saveAction;
	
	public ListAspectForm(Action saveAction) {
		this.saveAction = saveAction;
		initComponents();
	}
	
	public void setAspect(Aspect aspect, String[] categories) {
		if(aspect == null) {
			clearForm();
		} else {
			name.setText(aspect.getDirective());
			
			category.setEditable(true);
			category.setEnabled(true);
			category.setModel(new DefaultComboBoxModel(categories));
			category.getModel().setSelectedItem(aspect.getCategory());
			//category.setText(aspect.getCategory());
			description.setText(aspect.getDescription());
		}
	}
	
	public void setAspect(Aspect aspect) {
		if(aspect == null) {
			clearForm();
		} else {
			name.setText(aspect.getDirective());
			
			Object[] categories = new Object[] {aspect.getCategory()};
			category.setModel(new DefaultComboBoxModel(categories));
			category.setEditable(false);
			category.setEnabled(false);
			//category.setText(aspect.getCategory());
			description.setText(aspect.getDescription());
		}
	}
	
	public Aspect getAspect() {
		Aspect aspect = new Aspect();
		aspect.setDirective(name.getText());
		if(!category.isEditable())
			aspect.setCategory(category.getSelectedItem().toString());
		else
			aspect.setCategory(category.getEditor().getItem().toString());
		//aspect.setCategory(category.getText());
		aspect.setDescription(description.getText());
		
		return aspect;
	}
	
	public void clearForm() {
		name.setText(null);
		//category.setText(null);
		category.setSelectedIndex(-1);
		description.setText(null);
	}

	private void initComponents() {
		
		formHeading = new FormLabel();
		nameLabel = new FormLabel();
		categoryLabel = new FormLabel();
		descriptionLabel = new FormLabel();
		name = new CompletableJTextField();
		category = new JComboBox();
		//category = new FormLabel();
		scrollPane1 = new JScrollPane();
		description = new FadingTextArea();
		saveBtn = new JButton(saveAction);

		//======== this ========
		setOpaque(false);

		//---- formHeading ----
		formHeading.setText(Messages.getString(
				"PearReview.Moderator.aspects.aspect"));
		formHeading.setFont(new Font("Tahoma", Font.PLAIN, 14));
		
		//---- name ----
		name.addKeyListener(new SaveKeyListener());

		//---- nameLabel ----
		nameLabel.setText(Messages.getString(
				"PearReview.Moderator.aspects.directive"));

		//---- categoryLabel ----
		categoryLabel.setText(Messages.getString(
				"PearReview.Moderator.aspects.category"));

		//---- descriptionLabel ----
		descriptionLabel.setText(Messages.getString(
				"PearReview.Moderator.aspects.description"));

		//---- category ----
		category.setFont(new Font("Tahoma", Font.PLAIN, 12));
		if (System.getProperty("os.name").equals("Mac OS X")) {
			category.setBorder(BorderFactory.createEmptyBorder(
					0, 0, 0, 0));
		} else {
			category.setBorder(BorderFactory.createMatteBorder(
							1, 1, 1, 1, new Color(0x828790)));
		}
		
		//---- saveBtn ----
		saveBtn.setFont(new Font("Tahoma", Font.PLAIN, 12));

		//======== scrollPane1 ========
		{
			scrollPane1.setViewportView(description);
			scrollPane1.setPreferredSize(new Dimension(200,90));
		}

		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addGap(10)
					.addGroup(layout.createParallelGroup()
						.addComponent(formHeading, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addGroup(layout.createSequentialGroup()
							.addGroup(layout.createParallelGroup()
								.addComponent(nameLabel, 
										GroupLayout.PREFERRED_SIZE, 
										100, 
										100)
								.addComponent(categoryLabel, 
										GroupLayout.PREFERRED_SIZE, 
										100, 
										100)
								.addComponent(descriptionLabel, 
										GroupLayout.PREFERRED_SIZE, 
										100, 
										100))
							.addGap(57, 57, 57)
							.addGroup(layout.createParallelGroup()
									.addGroup(layout.createParallelGroup(
											GroupLayout.Alignment.LEADING)
									.addComponent(saveBtn, 
											GroupLayout.DEFAULT_SIZE, 
											GroupLayout.PREFERRED_SIZE, 
											GroupLayout.PREFERRED_SIZE)
									.addComponent(scrollPane1, 
											GroupLayout.DEFAULT_SIZE, 
											200, 
											400)
									.addComponent(category, 
											0, 
											200, 
											400)
									.addComponent(name, 
											GroupLayout.DEFAULT_SIZE, 
											200, 
											400)))))
					.addGap(10))
		);
		layout.setVerticalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(formHeading, 
							GroupLayout.PREFERRED_SIZE, 
							GroupLayout.DEFAULT_SIZE, 
							GroupLayout.PREFERRED_SIZE)
					.addGap(18, 18, 18)
					.addGroup(layout.createParallelGroup(
							GroupLayout.Alignment.BASELINE)
						.addComponent(nameLabel, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(name, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(layout.createParallelGroup(
							GroupLayout.Alignment.BASELINE)
						.addComponent(categoryLabel, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(category, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(layout.createParallelGroup()
						.addComponent(descriptionLabel, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.DEFAULT_SIZE, 
								GroupLayout.PREFERRED_SIZE)
						.addComponent(scrollPane1, 
								GroupLayout.PREFERRED_SIZE, 
								90, 
								GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(saveBtn, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE, 
								GroupLayout.PREFERRED_SIZE))
		);
	}
	
	private class SaveKeyListener implements KeyListener, Serializable {
		private static final long serialVersionUID = 4805036279156748029L;
		@Override
		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode() == KeyEvent.VK_ENTER) {
				saveBtn.doClick();
			}			
		}
		@Override
		public void keyReleased(KeyEvent e) {
			// do nothing			
		}
		@Override
		public void keyTyped(KeyEvent e) {
			// do nothing			
		}		
	}
}